#include<bits/stdc++.h>
using namespace std;
const int N=33;
int q,l,r,a[N],b[N];
long long dp[N][2][2][2][2];
long long solve(int l,int r){
	memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(dp,0,sizeof(dp));
	int tlh=0;while(l) a[++tlh]=l&1,l>>=1;
	tlh=0;while(r) b[++tlh]=r&1,r>>=1;
	dp[0][0][0][0][0]=1;
	for(int i=1;i<N;i++){
		for(int p=0;p<2;p++){
			for(int q=0;q<2;q++){
				if((!p)&&q) continue;
				for(int a1=0;a1<2;a1++){
					if(p<a[i]&&a1!=1) continue;
					if(p>a[i]&&a1!=0) continue;
					for(int a2=0;a2<2;a2++){
						if(p<b[i]&&a2!=0) continue;
						if(p>b[i]&&a2!=1) continue;
						for(int a3=0;a3<2;a3++){
							if(q<a[i]&&a3!=1) continue;
							if(q>a[i]&&a3!=0) continue;
							for(int a4=0;a4<2;a4++){
								if(q<b[i]&&a4!=0) continue;
								if(q>b[i]&&a4!=1) continue;
								for(int a5=0;a5<2;a5++){
									if(p==a[i]&&a1!=a5) continue;
									for(int a6=0;a6<2;a6++){
									if(p==b[i]&&a2!=a6) continue;
									for(int a7=0;a7<2;a7++){
									if(q==a[i]&&a3!=a7) continue;
									for(int a8=0;a8<2;a8++){
									if(q==b[i]&&a4!=a8) continue;
									dp[i][a1][a2][a3][a4]+=dp[i-1][a5][a6][a7][a8];
								}
								}
								}
								}
							}
						}
					}
				}
			}
		}
	}
	return dp[N-1][0][0][0][0];
}
signed main() {
	freopen("comb.in","r",stdin);
	freopen("comb.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>q;
	while(q--){
		cin>>l>>r;
		cout<<solve(l,r)<<'\n';
	}
	return 0;
}
